﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using log4net;
using NHibernate.Criterion;
using BidFunLib.Entity;


namespace BidFunLib.DAO
{
    class BidObjectDAO
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(BidObjectDAO));

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        public BidObject getBidObjectByLabel(String label)
        {
            BidObject bidObject = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<BidObject> objects = (IList<BidObject>)session.CreateQuery("from BidObject bo where bo.Label= ?")
                .SetString(0, label)
                .List<BidObject>();

                if (objects != null && objects.Count > 0)
                {
                    bidObject = objects.First();
                }
                transaction.Commit();
                session.Close();
                return bidObject;
            }
        }

        /// <summary>
        /// Save the object 
        /// </summary>
        public void saveBidObject(BidObject bidObject)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                log.Debug("Saving newBidObject " + bidObject.Label);
                Int32 newID = (Int32)session.Save(bidObject);
                bidObject.IdBidObject = newID;
                log.Debug("newBidObject " + bidObject.Label + " saved !");
                transaction.Commit();
                session.Close();
            }
        }

        public void updateBidObject(BidObject bidObject)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                if (bidObject.IdBidObject != -1)
                {
                    ITransaction transaction = session.BeginTransaction();
                    transaction.Begin();
                    log.Debug("Update newBidObject " + bidObject.Label);
                    session.Update(bidObject);
                    transaction.Commit();
                    session.Close();
                }
            }
        }



        public BidObject getObjectById(Int32 objectId)
        {
            BidObject bidObject = null;

            using (ISession session = NHibernateHelper.OpenSession())
            {
                ITransaction transaction = session.BeginTransaction();
                transaction.Begin();
                IList<BidObject> objects = (IList<BidObject>)session.CreateQuery("from BidObject bo where bo.IdBidObject= ?")
                .SetString(0, objectId.ToString())
                .List<BidObject>();

                if (objects != null && objects.Count > 0)
                {
                    bidObject = objects.First();
                }
                transaction.Commit();
                session.Close();
                return bidObject;
            }
        }
    }
}
